package dao.impl;

import dao.NewsDao;
import db.DBConstant;
import db.DBUtil;
import entity.News;
import entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl implements NewsDao {

    @Override
    public int insertNews(News news) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;

        String title = news.getTitle();
        String content = news.getContent();
        String author = news.getAuthor();
        String createTime = news.getCreateTime();

        /*System.out.println(title);
        System.out.println(content);
        System.out.println(author);
        System.out.println(createTime);*/

        String sql = "insert into news(title,content,author,createTime)values(?,?,?,?)";

        conn = DBUtil.getConnection();
        ps = conn.prepareStatement(sql);

        ps.setString(1,title);
        ps.setString(2,content);
        ps.setString(3,author);
        ps.setString(4,createTime);

        int result = ps.executeUpdate();
        DBUtil.close(null,ps,conn);

        return result;
    }

    @Override
    public List<News> findAllNews() throws SQLException {
        List<News> list = new ArrayList<News>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet set = null;
        conn = DBUtil.getConnection();
        String sql = "select * from news";
        ps = conn.prepareStatement(sql);
        set = ps.executeQuery();
        while (set.next()){
            int newsid = set.getInt(1);
            String title = set.getString(2);
            String content = set.getString(3);
            String author = set.getString(4);
            String createTime = set.getString(5);
            list.add(new News(newsid,title,content,author,createTime));
        }
        return list;
    }

    @Override
    public List<News> findLikes(String args) throws SQLException {
        List<News> list = new ArrayList<News>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet set = null;
        conn = DBUtil.getConnection();
        String sql = "select * from news where title like ?";
        ps = conn.prepareStatement(sql);
        ps.setString(1, "%"+args+"%");
        set = ps.executeQuery();
        while (set.next()){
            int newsid = set.getInt(1);
            String title = set.getString(2);
            String content = set.getString(3);
            String author = set.getString(4);
            String createTime = set.getString(5);
            list.add(new News(newsid,title,content,author,createTime));
        }
        return list;
    }

    @Override
    public List<News> findNewsByName(String username) throws SQLException {
        List<News> list = new ArrayList<News>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet set = null;
        conn = DBUtil.getConnection();
        String sql = "select * from news where author = ?";
        ps = conn.prepareStatement(sql);
        ps.setString(1,username);
        set = ps.executeQuery();
        while (set.next()){
            int newsid = set.getInt(1);
            String title = set.getString(2);
            String content = set.getString(3);
            String author = set.getString(4);
            String createTime = set.getString(5);
            list.add(new News(newsid,title,content,author,createTime));
        }
        return list;
    }
}
